# coding=utf-8
"""
Author: Mr.夏末
@Function: 功能名称
"""
import os

from class11.webkeys import Web
from common.Excel import Reader
from common.Logger import logger,path

# # 读取用例
# reader = Reader()
# filexcel = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib\cases', '电商项目用例.xlsx')
# # 打开一个excel
# reader.open_excel(filexcel)
# # 获取所有sheet
# sheets = reader.get_sheets()
# for sheet in sheets:
#     # 设置读取的sheet页面
#     reader.set_sheet(sheet)
#     # 读取当前sheet的所有行
#     lines = reader.readline()
#     # print(lines)
#     break
#
#
#     if line[3] == 'openbrowser':
#         web.openbrowser(line[4])
#         continue
#
#     if line[3] == 'geturl':
#         web.get_url(line[4])
#         continue
#     if line[3] == 'input':
#         web.input(line[4], line[5])
#         continue
#     if line[3] == 'click':
#         web.click(line[4])
#         continue
#     if line[3] == 'sleep':
#         web.sleep(line[4])
#         continue
#
#     if line[3] == 'gettext':
#         web.get_text(line[4])
#         continue
#
#     if line[3] == 'intoiframe':
#         web.Iframe(line[4])
#         continue
#
#     if line[3] == 'select':
#         web.select(line[4], line[5])
#         continue

# 上述代码使用反射实现

# 创建关键字对象
web = Web()

# 读取用例
reader = Reader()
filexcel = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'lib\cases', '电商项目用例.xlsx')
# 打开一个excel
reader.open_excel(filexcel)
# 获取所有sheet
sheets = reader.get_sheets()
# i = 0
# web.openbrowser()
for sheet in sheets:
    # i += 1
    # 设置读取的sheet页面
    reader.set_sheet(sheet)
    # 读取当前sheet的所有行
    lines = reader.readline()
    # if i < 4:
    #     continue
    # print(lines)

    for line in lines:
        logger.debug(line)
        if len(line[0]) > 0 or len(line[1]) > 0:
            # 分组信息不执行
            continue

        try:
            # 反射获取关键字
            func = getattr(web,line[3])
        except Exception as e:
            logger.error(e.__str__())
            continue

        try:
            # 获取参数
            params = line[4:]
            # 截掉第一个空字符串后面的所有元素
            params = params[:params.index('')]
            logger.info(params)
        except Exception as e:
            logger.exception(e)
            params = line[4:7]
        try:
            # 调用关键字
            func(*params)
        except Exception as e:
            logger.exception(e)
